/* Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package com.google.code.swingonfire.animation;

public class AnimationUtil
{

    /**
     * Calculates the fraction of the time within the duration (e.g. if the duration is 2, and the time is 1, this will
     * return 0.5). If the duration is undefined, the fraction is NaN. Will never return values < 0 or > 1. This is the
     * common way to calculate the current fraction of an animation
     * 
     * @param time the time of the animation
     * @param duration the duration of the animation
     * @return the fraction
     */
    public static double toFraction(double time, double duration)
    {
        if (isUndefined(duration))
        {
            return Double.NaN;
        }

        if (duration == 0)
        {
            return 1;
        }

        return Math.max(0, Math.min(1, time / duration));
    }

    /**
     * Bounds the specified result to the time and duration. If the animation is over (time >= duration) it returns NaN,
     * returns the result otherwise. This is the common way to limit an animation step result to the duration of the
     * animation.
     * 
     * @param result the aimed result
     * @param time the time of the animation
     * @param duration the duration of the animation
     * @return the boxed result
     */
    public static double animationResult(double result, double time, double duration)
    {
        return (time >= duration) ? Double.NaN : result;
    }

    /**
     * Returns true if the duration of the animation is undefined: <0, NaN or Inifinite
     * 
     * @param duration the duration of the animation
     * @return true if undefined
     */
    public static boolean isUndefined(double duration)
    {
        return (duration < 0) || (Double.isNaN(duration)) || (Double.isInfinite(duration));
    }

    /**
     * Returns true if the animation has finished as defined by the delay. An animation has finished if the delay is <0,
     * NaN or Infinite
     * 
     * @param delay the delay of the naimation
     * @return true if finished
     */
    public static boolean isFinished(double delay)
    {
        return (delay < 0) || (Double.isNaN(delay)) || (Double.isInfinite(delay));
    }

}
